package main.parsers.seasons;

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Map;

import main.database.Database;
import main.parsers.AbstractParser;
import main.parsers.Parsable;
import main.parsers.Parser;
import main.parsers.utils.Utils;

public class SeasonParser extends AbstractParser implements Parser 
{
	static final String path = site + "/calendar/";
	
	private Map<String, Integer> years;
	private Map<String, Integer> types;

	private static SeasonParser instance = new SeasonParser(Database.getInstance());
	
	private SeasonParser(Database db)
	{
		super(db);
	}
	
	public static SeasonParser getInstance()
	{
		return instance;
	}
	
	public void initialize()
	{
		types = Utils.loadTypes(false);
		years = Utils.loadYears(false);
	}
	
	@Override
	public Season getFromSite(String id) 
	{
		return null;
	}
	
	public Season getFromResultSet(ResultSet rs) 
	{
		try
		{
			if (rs.next()) return Season.get(rs, true);
		}
		catch(Exception e)
		{
			System.err.println("Unable to get season from database");
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Season getFromDataBase(int id) 
	{
		ResultSet rs = null;
		try 
		{
			rs = db.executeQuery("select.season", new Object[] {id});
			return getFromResultSet(rs);
		} 
		catch (Exception e) 
		{
			System.err.println("Unable to execute select season query");
			e.printStackTrace();
		}
		finally
		{
			closeResultSet(rs);
		}
		return null;
	}

	@Override
	public boolean updateToDataBase(Parsable o) 
	{
		if (o == null || !(o instanceof Season)) return false;
		
		Season s = (Season)o;
		try
		{
			Object[] params = { years.get(s.getYear()), types.get(s.getType()), s.getId() };
			
			ResultSet rs = db.executeQuery("select.season", new Object[] {s.getId()});
			if (rs.next()) 
			{
				Season season = getFromResultSet(rs);
				if (!s.equals(season)) return db.executeChange("update.season", params) > 0;
			}
			else return db.executeChange("insert.season", params) > 0;
		}
		catch (Exception e) 
		{
			System.err.println("Unable to update season");
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int updateAll() 
	{
		int count = 0;
		BufferedReader reader = getReader(path);
		String line;
		try
		{
			while ((line=reader.readLine()) != null)
			{
				if (line.contains("content_top_menu"))
				{

					pass(reader, 1);
					while((line=reader.readLine()) != null)
					{
						if (line.contains("<li"))
						{
							if (line.contains("documents")) continue;

							int start = line.indexOf("calendar") + 9;
							int end = line.indexOf('/', start);
							int id = Integer.parseInt(line.substring(start, end));

							line = getTagContent(line);
							int pos = line.lastIndexOf(' ');
							
							String type = line.substring(0, pos);
							String year = line.substring(pos + 1);
							Season season = new Season(id, type, year);

							if (!types.containsKey(type))
							{
								Utils.makeType(type);
								types = Utils.loadTypes(false);
							}
							
							if (!years.containsKey(year))
							{
								Utils.makeYear(year);
								years = Utils.loadYears(false);
							}
							
							updateToDataBase(season);
							count++;
						}
						else break;
					}
					break;
				}
			}
		}
		catch (IOException e) 
		{
			System.err.println("Unable to read page");
			e.printStackTrace();
		} 
		catch (Exception e) 
		{
			System.err.println("Unable to add season");
			e.printStackTrace();
		}
		finally
		{
			closeReader(reader);
		}
		return count;
	}

}
